package com.ysy.logistics.service.system.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysy.logistics.entities.system.vo.TreeSelectNode;
import com.ysy.logistics.entities.system.MenuNode;
import com.ysy.logistics.entities.system.vo.RoleMenuTreeVO;
import com.ysy.logistics.mapper.system.MenuNodeMapper;
import com.ysy.logistics.mapper.system.RoleMenuMapper;
import com.ysy.logistics.service.system.MenuNodeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassDescription:
 * @Author:姚斯羽
 * @Create:2025/3/19 10:16
 **/
@Service
public class MenuNodeServiceImpl extends ServiceImpl<MenuNodeMapper, MenuNode> implements MenuNodeService {
    @Resource
    private MenuNodeMapper menuNodeMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;


    private List<TreeSelectNode> createTreeSelectNodeByPid(Integer id) {
        if (id == null){
            return null;
        }
        LambdaQueryWrapper<MenuNode> lqw = new LambdaQueryWrapper<>();
        lqw.select(
                MenuNode::getId,
                MenuNode::getCode,
                MenuNode::getName)
                .eq(MenuNode::getParentId,id);
        List<MenuNode> menuNodes = menuNodeMapper.selectList(lqw);
        if (CollUtil.isNotEmpty(menuNodes)){
            List<TreeSelectNode> treeSelectNodes = new ArrayList<>();
            //循环节点
            for (MenuNode menuNode : menuNodes) {
                //创建子节点
                TreeSelectNode treeSelectNode = new TreeSelectNode();
                treeSelectNode.setId(menuNode.getCode());
                treeSelectNode.setLabel(menuNode.getName());
                //递归查询子节点
                List<TreeSelectNode> children = createTreeSelectNodeByPid(menuNode.getId().intValue());
                treeSelectNode.setChildren(children);
                treeSelectNodes.add(treeSelectNode);
            }
            return treeSelectNodes;

        }
        return null;
    }

    private List<MenuNode> getMenusByPid(Integer id) {
        if (id == null){
            return null;
        }
        LambdaQueryWrapper<MenuNode> lqw = new LambdaQueryWrapper<>();
        lqw.select()
                .eq(MenuNode::getParentId,id).orderByAsc(MenuNode::getNum);
        List<MenuNode> menuNodes = menuNodeMapper.selectList(lqw);
        if (CollUtil.isNotEmpty(menuNodes)){
            for (int i = 0; i < menuNodes.size(); i++) {
                menuNodes.get(i).setChildren(getMenusByPid(menuNodes.get(i).getId().intValue()));
            }

        }
        return menuNodes;
    }

    /**
     * 批量删除菜单
     * @param ids
     */
    private void deleteBatch(List<Integer> ids) {
        if (CollectionUtil.isEmpty(ids)){
            return;
        }
        //查询出子菜单
        LambdaQueryWrapper<MenuNode> lqw = new LambdaQueryWrapper<>();
        lqw.select(MenuNode::getId).in(MenuNode::getParentId,ids);
        List<Integer> menuNodes = menuNodeMapper.selectObjs(lqw);
        deleteBatch(menuNodes);
        menuNodeMapper.deleteByIds(ids);
    }


    @Override
    public List<TreeSelectNode> getMenuTree() {
        return createTreeSelectNodeByPid(0);
    }

    @Override
    public List<MenuNode> getMenus() {
        return getMenusByPid(0);
    }

    @Override
    public void clearById(Long id) {
        List<Integer> ids = new ArrayList<>();
        ids.add(id.intValue());
        deleteBatch(ids);
    }

    @Override
    public List<RoleMenuTreeVO> getRoleMenuTree( List<Long> checkedMenuIds) {
        List<MenuNode> menusByPid = getMenusByPid(0);
        return RoleMenuTreeVO.convertToRoleMenuTreeVO(menusByPid,checkedMenuIds);
    }
}
